package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.pudao.PuDaoSpecialOrder;
import com.ucredit.babel.common.model.third.pudao.PuDaoSpecialOrderData;
import com.ucredit.babel.common.model.third.pudao.PuDaoSpecialOrderRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 京东盘古黑名单
 *
 * @author renzhanzheng
 */

public class JdPgBlankListParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 京东盘古黑名单
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */

    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        PuDaoSpecialOrderRecord obj = (PuDaoSpecialOrderRecord)model;
        PuDaoSpecialOrderData data = obj.getData() == null ? new PuDaoSpecialOrderData() : obj.getData();
        PuDaoSpecialOrder scoreData = data.getData() == null ? new PuDaoSpecialOrder() : data.getData();

        StringBuilder sb = getBaseInfo(obj, successful);

        //返回编码
        sb.append(data.getCode() == null ? "" : data.getCode()).append(CRT001);
        //返回信息
        sb.append(data.getMsg() == null ? "" : data.getMsg()).append(CRT001);
        //是否命中
        sb.append(scoreData.getIsTargeted() == null ? "" : scoreData.getIsTargeted()).append(CRT001);
        //涉黑程度
        sb.append(scoreData.getTargetLev() == null ? "" : scoreData.getTargetLev()).append(CRT001);
        //涉黑类型
        sb.append(scoreData.getField() == null ? "" : scoreData.getField()).append(CRT001);

        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb.toString()).replace("\n","").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_jingdong_blacklist_inc";
    }

    @Override
    public String getType() {
        return RecordType.PD_SPECIAL_ORDER_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "朴道京东盘古黑名单";
    }
}
